package com.source.baseData.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.SlaughterHouse;
import com.source.baseData.entity.Stalls;
import com.source.baseData.vo.ToMarketGraphVO;
import com.source.baseData.entity.ToMarket;
import com.source.baseData.service.ToMarketService;
import com.source.utils.CommonResult;
import com.source.utils.ExcelUtil;
import com.source.utils.ResultEnum;
import com.source.baseData.vo.ToMarketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Feng
 * @since 2021-02-12
 */
@Api(tags = "去向市场基础数据管理")
@RestController
@RequestMapping("/baseData/to-market")
public class ToMarketController {

    @Resource
    private ToMarketService toMarketService;

    @ApiOperation(value = "上传Excel", produces = "application/json;charset=utf-8")
    @PostMapping("/upload-excel")
    public void upload(MultipartFile file) throws IOException {
        ExcelUtil.uploadExcel(file, ToMarket.class, toMarketService);
    }

    @ApiOperation(value = "查询所有市场去向信息", notes = "- 只查询审核通过的数据")
    @GetMapping
    public CommonResult<List<ToMarketVO>> find() {
        List<ToMarketVO> list = toMarketService.find(1);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有市场去向信息-分页", notes = "- 只查询审核通过的数据")
    @GetMapping("/find-page")
    public CommonResult<Page<ToMarketVO>> find(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<ToMarketVO> page = toMarketService.find(1, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND, page);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "根据去向市场id查询信息", notes = "查询所有状态的数据")
    @GetMapping(value = "/{id}")
    public CommonResult<ToMarketVO> find(@PathVariable("id") String id) {
        ToMarketVO tm = toMarketService.findById(id);
        if (tm == null) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<ToMarketVO>(ResultEnum.SUCCESS, tm);
    }

    @ApiOperation(value = "增加去向市场信息", notes = "- 等待管理员审核")
    @PostMapping
    public CommonResult<String> add(@RequestBody @Validated ToMarket toMarket) {
        boolean res = toMarketService.add(toMarket);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "修改去向市场信息", notes = "- 不用审核")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody ToMarket toMarket) {
        boolean res = toMarketService.modify(toMarket);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "删除信息", notes = "- 等待管理员审核")
    @DeleteMapping("/{id}")
    public CommonResult<String> deleteById(@PathVariable("id") String id) {
        boolean res = toMarketService.deleteById(id);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "批量删除信息", notes = "- 等待管理员审核 \n - 参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/delete-batch")
    public CommonResult<String> delete(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        boolean res = toMarketService.deleteBatch(ids);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "按市场名称模糊查询", notes = "- 只查询审核通过的数据")
    @GetMapping(value = "list-by-slaughter-house-name")
    public CommonResult<List<ToMarketVO>> findBySlaughterHouseName(String name) {
        if (StringUtils.isEmpty(name)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<ToMarketVO> list = toMarketService.findBySlaughterHouseName(name);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "按市场名称模糊查询 并分页", notes = "- 只查询审核通过的数据")
    @GetMapping(value = "page-by-slaughter-house-name")
    public CommonResult<Page<ToMarketVO>> findBySlaughterHouseName(String name, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {

        if (StringUtils.isEmpty(name)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<ToMarketVO> page = toMarketService.findBySlaughterHouseName(name, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND, page);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "按市场档口名称模糊查询", notes = "- 只查询审核通过的数据")
    @GetMapping(value = "list-by-stall-name")
    public CommonResult<List<ToMarketVO>> findByStallName(String stallName) {

        if (StringUtils.isEmpty(stallName)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<ToMarketVO> list = toMarketService.findByStallName(stallName);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "按市场档口名称模糊查询", notes = "- 只查询审核通过的数据")
    @GetMapping(value = "page-by-stall-name")
    public CommonResult<Page<ToMarketVO>> findByStallName(String stallName, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {

        if (StringUtils.isEmpty(stallName)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        Page<ToMarketVO> page = toMarketService.findByStallName(stallName, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND, page);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "按省返回所有市")
    @GetMapping(value = "/get-cities")
    public CommonResult<List<String>> getCities(String province) {
        if (StringUtils.isEmpty(province)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<String> list = toMarketService.getCities(province);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "按省市返回所有区")
    @GetMapping(value = "/get-districts")
    public CommonResult<List<String>> getDistricts(String province, String city) {
        if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<String> list = toMarketService.getDistricts(province, city);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "按省市区返回市场")
    @GetMapping(value = "/{province}/{city}/{district}")
    public CommonResult<List<ToMarketVO>> findMarket(@PathVariable("province") String province, @PathVariable("city") String city, @PathVariable("district") String district) {
        if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city) || StringUtils.isEmpty(district)) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        List<ToMarketVO> list = toMarketService.find(province, city, district);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "根据屠宰场id返回最近半年档口数")
    @GetMapping(value = "/stalls-count-by-slaughterhouse-id")
    public CommonResult<ToMarketGraphVO> stallsCount(String slaughterHouseId) {
        ToMarketGraphVO res = toMarketService.getPastHalfYearStallNums(slaughterHouseId);
        return new CommonResult<>(ResultEnum.SUCCESS, res);
    }

    @Deprecated
    @ApiOperation(value = "管理员审核 - 发布通过 ", notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @PostMapping(value = "/agree-publish")
    public CommonResult<String> agreePublishBatch(String ids) {
        if (ids == null) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        boolean res = toMarketService.updateBatchStatusByIds(1, ids);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @Deprecated
    @ApiOperation(value = "管理员审核 - 删除通过 ", notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping(value = "/agree-delete")
    public CommonResult<String> agreeDeleteBatch(String ids) {
        if (ids == null) {
            return new CommonResult<>(ResultEnum.PARAMS_NULL);
        }
        boolean res = toMarketService.updateBatchStatusByIds(0, ids);
        if (res) {
            return new CommonResult<>(ResultEnum.SUCCESS);
        } else {
            return new CommonResult<>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "获取发布待审核列表")
    @GetMapping("/list-publishing")
    public CommonResult<List<ToMarketVO>> findPublishing() {
        List<ToMarketVO> list = toMarketService.find(2);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "获取发布待审核列表 -分页")
    @GetMapping("/page-publishing")
    public CommonResult<Page<ToMarketVO>> findPublishing(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<ToMarketVO> page = toMarketService.find(2, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "获取删除待审核列表")
    @GetMapping("/list-deleting")
    public CommonResult<List<ToMarketVO>> findDeleting() {
        List<ToMarketVO> list = toMarketService.find(3);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "获取删除待审核列表 -分页")
    @GetMapping("/page-deleting")
    public CommonResult<Page<ToMarketVO>> findDeleting(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<ToMarketVO> page = toMarketService.find(3, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "获取待审核列表", notes = "包括删除待审核和发布待审核")
    @GetMapping("/list-toBeReviewed")
    public CommonResult<List<ToMarketVO>> toBeReviewed() {
        List<ToMarketVO> list = toMarketService.toBeReviewed();
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "获取待审核列表 -分页", notes = "包括删除待审核和发布待审核")
    @GetMapping("/page-toBeReviewed")
    public CommonResult<Page<ToMarketVO>> toBeReviewed(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<ToMarketVO> page = toMarketService.toBeReviewed(current, size);
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "正常去向市场 模糊查询")
    @GetMapping("/list-like")
    public CommonResult<List<ToMarketVO>> like(String str) {
        if (StringUtils.isEmpty(str)) {
            return find();
        }
        List<ToMarketVO> list = toMarketService.like(str);
        if (list.isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "正常去向市场 模糊查询并分页")
    @GetMapping("/page-like")
    public CommonResult<Page<ToMarketVO>> like(String str, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(str)) {
            return find(current, size);
        }
        Page<ToMarketVO> page = toMarketService.like(str, current, size);
        if (page.getRecords().isEmpty()) {
            return new CommonResult<>(ResultEnum.DATA_NOT_FOUND, page);
        }
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "待审核去向市场 模糊查询")
    @GetMapping("/list-toBeReviewed-like")
    public CommonResult<List<ToMarketVO>> toBeReviewedLike(String str) {
        if (StringUtils.isEmpty(str)) {
            return find();
        }
        List<ToMarketVO> list = toMarketService.toBeReviewedLike(str);
        return new CommonResult<>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "待审核去向市场 模糊查询并分页")
    @GetMapping("/page-toBeReviewed-like")
    public CommonResult<Page<ToMarketVO>> toBeReviewedLike(@RequestParam(value = "keyWord", required = false) String str, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (null == str) {
            return find(current, size);
        }
        Page<ToMarketVO> page = toMarketService.toBeReviewedLike(str, current, size);
        return new CommonResult<>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "管理员审核 ", notes = "单个审核，传id")
    @PostMapping(value = "/confirm/{id}")
    public CommonResult<String> confirm(@PathVariable String id) {
        ToMarket byId = toMarketService.getById(id);
        try {
            if(byId.getStatus() == 3){
                byId.setStatus(0);
                if (toMarketService.updateById(byId) && toMarketService.removeById(id)) {
                    return new CommonResult<String>(ResultEnum.SUCCESS);
                } else {
                    return new CommonResult<String>(ResultEnum.ERROR);
                }
            }else if(byId.getStatus() == 2) {
                byId.setStatus(1);
                if(toMarketService.updateById(byId)){
                    return new CommonResult<String>(ResultEnum.SUCCESS);
                } else {
                    return new CommonResult<String>(ResultEnum.ERROR);
                }
            } else {
                return new CommonResult<String>(ResultEnum.ERROR, "该数据不处于待删除或待通过状态！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR, "操作异常！");
        }
    }

    @ApiOperation(value = "管理员审核（审核不通过） ", notes = "单个审核，传id，如果是待删除状态，执行后会恢复正常\n如果是待发布状态，执行后会删除数据")
    @PostMapping(value = "/disConfirm/{id}")
    public CommonResult<String> disConfirm(@PathVariable String id) {
        ToMarket byId = toMarketService.getById(id);
        try {
            if(byId.getStatus() == 3){
                byId.setStatus(1);
                if (toMarketService.updateById(byId)) {
                    return new CommonResult<String>(ResultEnum.SUCCESS);
                } else {
                    return new CommonResult<String>(ResultEnum.ERROR);
                }
            }else if(byId.getStatus() == 2) {
                byId.setStatus(0);
                if(toMarketService.updateById(byId) && toMarketService.removeById(id)){
                    return new CommonResult<String>(ResultEnum.SUCCESS);
                } else {
                    return new CommonResult<String>(ResultEnum.ERROR);
                }
            } else {
                return new CommonResult<String>(ResultEnum.ERROR, "该数据不处于待删除或待通过状态！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR, "操作异常！");
        }
    }

}

